﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmSpendList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int Spend_ID = 0;

        public frmSpendList()
        {
            InitializeComponent();
            btnDel.Enabled = Program.isAdmin;
            DisplayFrame();

            LoadData();
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Spend_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Code", "Phiếu chi", "", ColumnWidth.Code2, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Date", "Ngày chi", "C", ColumnWidth.Date, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Total", "Số tiền", "R", ColumnWidth.TotalPrice, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Reason", "Lý do", "", ColumnWidth.Description, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Receive", "Tên người nhận", "", ColumnWidth.PersonName, true, true);
            UISetting.AddColumn(ref dgvList1, "Spend_Type", "Chi trả", "", ColumnWidth.Description, true, true);
        }
        private void LoadData()
        {
            DataTable dt = SpendDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["Spend_ID"].Value = dt.Rows[i]["Spend_ID"].ToString();
                dgvList1.Rows[i].Cells["Spend_Code"].Value = dt.Rows[i]["Spend_Code"].ToString();
                dgvList1.Rows[i].Cells["Spend_Date"].Value = Convert.ToDateTime(dt.Rows[i]["Spend_Date"]).ToString("dd/MM/yyyy");
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Spend_Total"].Value = Convert.ToDecimal(dt.Rows[i]["Spend_Total"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Spend_Reason"].Value = dt.Rows[i]["Spend_Reason"].ToString();
                dgvList1.Rows[i].Cells["Spend_Receive"].Value = dt.Rows[i]["Spend_Receive"].ToString();
                if(dt.Rows[i]["Spend_Type"].ToString() == "NCC")
                dgvList1.Rows[i].Cells["Spend_Type"].Value = "Nhà cung cấp";
                else
                dgvList1.Rows[i].Cells["Spend_Type"].Value = "Khách hàng";
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                Spend_ID = Convert.ToInt32(dgvList1["Spend_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                if (MessageBox.Show(Message.Delete_Question, Message.Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (SpendDB.Delete(Spend_ID))
                    {
                        Program.taskbarNotifier.Show(Message.Info, Message.Delete_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(Message.Delete_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                Spend_ID = Convert.ToInt32(dgvList1["Spend_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                string Spend_Type = "";
                if (dgvList1["Spend_Type", dgvList1.SelectedCells[0].RowIndex].Value.ToString() == "Nhà cung cấp")
                    Spend_Type = "NCC";
                else
                    Spend_Type = "KH";

                Form f = new frmSpend(Spend_ID, Spend_Type);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmSpend(0, "NCC");
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex>=0)
            btnEdit_Click(null,null);
        }


    }
}